package com.framework.modules.sys.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.annotation.DataFilter;
import com.framework.common.utils.Constant;
import com.framework.dao.YHBDao.sys.YhbSysUserDao;
import com.framework.modules.sys.service.YhbDeptService;
import com.framework.modules.sys.service.YhbUserRoleClassService;
import com.framework.modules.sys.shiro.ShiroUtils;
import com.framework.modules.sys.service.YhbSysUserService;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.yhbEntity.sys.YhbDept;
import com.framework.yhbEntity.sys.YhbSysUser;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("yhbSysUserService")
public class YhbSysUserServiceImpl extends ServiceImpl<YhbSysUserDao,YhbSysUser> implements YhbSysUserService {


    @Autowired
    private YhbUserRoleClassService yhbUserRoleClassService;
    @Autowired
    private YhbDeptService sysDeptService;

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return baseMapper.queryAllMenuId(userId);
    }

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String) params.get("username");
        Page<YhbSysUser> page = this.selectPage(new Query<YhbSysUser>(params).getPage(),
                new EntityWrapper<YhbSysUser>()
                        .like(StringUtils.isNotBlank(username), "username", username).addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER)));

        for (YhbSysUser yhbSysUser : page.getRecords()) {
            YhbDept sysDeptEntity = sysDeptService.selectById(yhbSysUser.getDeptId());
            yhbSysUser.setDeptName(sysDeptEntity.getName());
        }

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(YhbSysUser user) {
        user.setCreateDate(new Date());
        // sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        this.insert(user);

        // 保存用户与角色关系
        yhbUserRoleClassService.saveOrUpdate(user.getSysUserId(), user.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YhbSysUser user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        } else {
            user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        }
        this.updateById(user);

        // 保存用户与角色关系
        yhbUserRoleClassService.saveOrUpdate(user.getSysUserId(), user.getRoleIdList());
    }

    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        YhbSysUser sysUser = new YhbSysUser();
        sysUser.setPassword(newPassword);
        return this.update(sysUser, new EntityWrapper<YhbSysUser>().eq("sys_user_id", userId).eq("password", password));
    }

    @Override
    public List<YhbSysUser> querySysUserList() {
        return baseMapper.selectList(null);
    }

    @Override
    public YhbSysUser getUserByNameAndMobile(String name, String mobile) {
        YhbSysUser yhbSysUser=new YhbSysUser();
        yhbSysUser.setUsername(name);
        yhbSysUser.setMobile(mobile);
        YhbSysUser user = baseMapper.selectOne(yhbSysUser);
        return user;
    }
}
